package cn.bestick.weightrecord.util;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: Bestick
 * Date: 2016-09-14
 * Time: 22:32
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 Bestick All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
public class ReNameUtil {

    public static void updateFileNames(String url) {
        File file = new File(url);
        //判断文件目录是否存在，且是文件目录，非文件
        if (file.exists() && file.isDirectory()) {
            File[] childFiles = file.listFiles();
            String path = file.getAbsolutePath();
            for (File childFile : childFiles) {
                //如果是文件
                String pre = "";
                String sub = "";
                if (childFile.isFile()) {
                    String oldName = childFile.getName();

                    StringBuffer stringBuilder = new StringBuffer("");
                    BufferedReader br = null;
                    BufferedWriter bw = null;
                    int nowLine = 0;
                    if (oldName.substring(0,oldName.indexOf(".")).length() < 5) {
                        System.out.println(oldName.substring(0,oldName.indexOf(".")));
                        try {
                            // 读取文件
                            br = new BufferedReader(new InputStreamReader(new FileInputStream(childFile), "GBK"));
                            String line = "";
                            while ((line = br.readLine()) != null && nowLine < 1) {
                                nowLine++;
//                            System.out.println(oldName + ">>>>>>>>>>>>>" + line);
                                    pre = line.replaceAll("\\d+","").trim();
                                    sub = oldName;
//                                System.out.println(pre + sub);
//                                    System.out.println(oldName + ">>>>>>" + oldName.substring(0,oldName.indexOf(".")));
//                            if (line.contains("#")) {
//                                System.out.print(oldName + ">>>>>>>>>>>>>>>>>>>");
//                                pre = line.substring(5).replaceAll("\\d+", "").trim();
//                                System.out.print(pre);
//                                System.out.print(">>>>>>>>>>>>>>>>>>>job:");
//                            }
//                            if (line.contains("-")) {
//                                pre = oldName.substring(0,oldName.indexOf("."));
//                                sub = " " + line.substring(line.indexOf("-")).trim();
//                                System.out.println(pre+sub);
//                            }
                                stringBuilder.append(line);
                                stringBuilder.append("\n");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (null != bw) {
                                    bw.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            try {
                                if (null != br) {
                                    br.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
//                String newName = pre + sub;
//                childFile.renameTo(new File(path + "\\" +  newName));
//                System.out.println(path + "\\" +  newName);
            }
        }
    }

    public static void main(String[] args) {
        updateFileNames("C:\\Users\\Administrator\\Desktop\\before");
    }
}
